<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>roster_config_manager.config_lib &mdash; Roster Config Manager v0.18 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     'v0.18',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Roster Config Manager v0.18 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">Roster Config Manager v0.18 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for roster_config_manager.config_lib</h1><div class="highlight"><pre>
<span class="c">#!/usr/bin/python</span>

<span class="c"># Copyright (c) 2009, Purdue University</span>
<span class="c"># All rights reserved.</span>
<span class="c">#</span>
<span class="c"># Redistribution and use in source and binary forms, with or without</span>
<span class="c"># modification, are permitted provided that the following conditions are met:</span>
<span class="c">#</span>
<span class="c"># Redistributions of source code must retain the above copyright notice, this</span>
<span class="c"># list of conditions and the following disclaimer.</span>
<span class="c">#</span>
<span class="c"># Redistributions in binary form must reproduce the above copyright notice, this</span>
<span class="c"># list of conditions and the following disclaimer in the documentation and/or</span>
<span class="c"># other materials provided with the distribution.</span>
<span class="c">#</span>
<span class="c"># Neither the name of the Purdue University nor the names of its contributors</span>
<span class="c"># may be used to endorse or promote products derived from this software without</span>
<span class="c"># specific prior written permission.</span>
<span class="c">#</span>
<span class="c"># THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot;</span>
<span class="c"># AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
<span class="c"># IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE</span>
<span class="c"># DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE</span>
<span class="c"># FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL</span>
<span class="c"># DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR</span>
<span class="c"># SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER</span>
<span class="c"># CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,</span>
<span class="c"># OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</span>
<span class="c"># OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>

<span class="sd">&quot;&quot;&quot;This module conains all of the logic to check dns servers.&quot;&quot;&quot;</span>

<span class="n">__copyright__</span> <span class="o">=</span> <span class="s">&#39;Copyright (C) 2009, Purdue University&#39;</span>
<span class="n">__license__</span> <span class="o">=</span> <span class="s">&#39;BSD&#39;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s">&#39;#TRUNK#&#39;</span>

<span class="kn">import</span> <span class="nn">ConfigParser</span>
<span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">iscpy</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">roster_core</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="kn">import</span> <span class="nn">tarfile</span>

<span class="kn">from</span> <span class="nn">fabric</span> <span class="kn">import</span> <span class="n">api</span> <span class="k">as</span> <span class="n">fabric_api</span>
<span class="kn">from</span> <span class="nn">fabric</span> <span class="kn">import</span> <span class="n">network</span> <span class="k">as</span> <span class="n">fabric_network</span>
<span class="kn">from</span> <span class="nn">fabric</span> <span class="kn">import</span> <span class="n">state</span> <span class="k">as</span> <span class="n">fabric_state</span>
<span class="kn">from</span> <span class="nn">fabric.exceptions</span> <span class="kn">import</span> <span class="n">NetworkError</span> <span class="k">as</span> <span class="n">FabricNetworkError</span>
<span class="kn">from</span> <span class="nn">roster_core</span> <span class="kn">import</span> <span class="n">config</span>
<span class="kn">from</span> <span class="nn">roster_core</span> <span class="kn">import</span> <span class="n">errors</span>


<span class="n">roster_core</span><span class="o">.</span><span class="n">core</span><span class="o">.</span><span class="n">CheckCoreVersionMatches</span><span class="p">(</span><span class="n">__version__</span><span class="p">)</span>

<div class="viewcode-block" id="ConfigManagerError"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigManagerError">[docs]</a><span class="k">class</span> <span class="nc">ConfigManagerError</span><span class="p">(</span><span class="n">errors</span><span class="o">.</span><span class="n">CoreError</span><span class="p">):</span>
  <span class="k">pass</span>
</div>
<div class="viewcode-block" id="ExporterNoFileError"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ExporterNoFileError">[docs]</a><span class="k">class</span> <span class="nc">ExporterNoFileError</span><span class="p">(</span><span class="n">ConfigManagerError</span><span class="p">,</span> <span class="n">errors</span><span class="o">.</span><span class="n">UserError</span><span class="p">):</span>
  <span class="k">pass</span>
</div>
<div class="viewcode-block" id="ExporterFileError"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ExporterFileError">[docs]</a><span class="k">class</span> <span class="nc">ExporterFileError</span><span class="p">(</span><span class="n">ConfigManagerError</span><span class="p">,</span> <span class="n">errors</span><span class="o">.</span><span class="n">UserError</span><span class="p">):</span>
  <span class="k">pass</span>
</div>
<div class="viewcode-block" id="ExporterFileNameError"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ExporterFileNameError">[docs]</a><span class="k">class</span> <span class="nc">ExporterFileNameError</span><span class="p">(</span><span class="n">ExporterFileError</span><span class="p">):</span>
  <span class="k">pass</span>
</div>
<div class="viewcode-block" id="ExporterListFileError"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ExporterListFileError">[docs]</a><span class="k">class</span> <span class="nc">ExporterListFileError</span><span class="p">(</span><span class="n">ExporterFileError</span><span class="p">):</span>
  <span class="k">pass</span>
</div>
<div class="viewcode-block" id="ExporterAuditIdError"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ExporterAuditIdError">[docs]</a><span class="k">class</span> <span class="nc">ExporterAuditIdError</span><span class="p">(</span><span class="n">ConfigManagerError</span><span class="p">,</span> <span class="n">errors</span><span class="o">.</span><span class="n">UserError</span><span class="p">):</span>
  <span class="k">pass</span>
</div>
<div class="viewcode-block" id="ServerCheckError"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ServerCheckError">[docs]</a><span class="k">class</span> <span class="nc">ServerCheckError</span><span class="p">(</span><span class="n">ConfigManagerError</span><span class="p">,</span> <span class="n">errors</span><span class="o">.</span><span class="n">UserError</span><span class="p">):</span>
  <span class="k">pass</span>
</div>
<div class="viewcode-block" id="QueryCheckError"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.QueryCheckError">[docs]</a><span class="k">class</span> <span class="nc">QueryCheckError</span><span class="p">(</span><span class="n">ConfigManagerError</span><span class="p">,</span> <span class="n">errors</span><span class="o">.</span><span class="n">UserError</span><span class="p">):</span>
  <span class="k">pass</span>
</div>
<div class="viewcode-block" id="ConfigLib"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib">[docs]</a><span class="k">class</span> <span class="nc">ConfigLib</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;This class checks a DNS server for the functionality required to push</span>
<span class="sd">  zone files to it&quot;&quot;&quot;</span>

  <span class="n">ToolList</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;named-checkzone&#39;</span><span class="p">,</span> <span class="s">&#39;named-compilezone&#39;</span><span class="p">,</span> 
              <span class="s">&#39;named-checkconf&#39;</span><span class="p">,</span> <span class="s">&#39;tar&#39;</span><span class="p">]</span>

  <span class="n">COMMANDS_CONVERT_DICT</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">&#39;check-dup-records&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;fail&#39;</span><span class="p">:</span> <span class="s">&#39;fail&#39;</span><span class="p">,</span> <span class="s">&#39;warn&#39;</span><span class="p">:</span> <span class="s">&#39;warn&#39;</span><span class="p">,</span> <span class="s">&#39;ignore&#39;</span><span class="p">:</span> <span class="s">&#39;ignore&#39;</span><span class="p">},</span>
    <span class="s">&#39;check-mx&#39;</span><span class="p">:</span>          <span class="p">{</span><span class="s">&#39;fail&#39;</span><span class="p">:</span> <span class="s">&#39;fail&#39;</span><span class="p">,</span> <span class="s">&#39;warn&#39;</span><span class="p">:</span> <span class="s">&#39;warn&#39;</span><span class="p">,</span> <span class="s">&#39;ignore&#39;</span><span class="p">:</span> <span class="s">&#39;ignore&#39;</span><span class="p">},</span>
    <span class="s">&#39;check-mx-cname&#39;</span><span class="p">:</span>    <span class="p">{</span><span class="s">&#39;fail&#39;</span><span class="p">:</span> <span class="s">&#39;fail&#39;</span><span class="p">,</span> <span class="s">&#39;warn&#39;</span><span class="p">:</span> <span class="s">&#39;warn&#39;</span><span class="p">,</span> <span class="s">&#39;ignore&#39;</span><span class="p">:</span> <span class="s">&#39;ignore&#39;</span><span class="p">},</span>
    <span class="s">&#39;check-srv-cname&#39;</span><span class="p">:</span>   <span class="p">{</span><span class="s">&#39;fail&#39;</span><span class="p">:</span> <span class="s">&#39;fail&#39;</span><span class="p">,</span> <span class="s">&#39;warn&#39;</span><span class="p">:</span> <span class="s">&#39;warn&#39;</span><span class="p">,</span> <span class="s">&#39;ignore&#39;</span><span class="p">:</span> <span class="s">&#39;ignore&#39;</span><span class="p">},</span>
    <span class="s">&#39;check-wildcard&#39;</span><span class="p">:</span>    <span class="p">{</span><span class="s">&#39;yes&#39;</span><span class="p">:</span> <span class="s">&#39;warn&#39;</span><span class="p">,</span> <span class="s">&#39;no&#39;</span><span class="p">:</span> <span class="s">&#39;ignore&#39;</span><span class="p">},</span>

    <span class="s">&#39;check-names&#39;</span><span class="p">:</span>       <span class="p">{</span><span class="s">&#39;flag&#39;</span><span class="p">:</span> <span class="s">&#39;-k&#39;</span><span class="p">,</span> <span class="s">&#39;default&#39;</span><span class="p">:</span> <span class="s">&#39;fail&#39;</span><span class="p">},</span>
    <span class="s">&#39;check-integrity&#39;</span><span class="p">:</span>   <span class="p">{</span><span class="s">&#39;yes&#39;</span><span class="p">:</span> <span class="s">&#39;full&#39;</span><span class="p">,</span> <span class="s">&#39;no&#39;</span><span class="p">:</span> <span class="s">&#39;none&#39;</span><span class="p">}</span>
  <span class="p">}</span>

  <span class="n">CHECKZONE_ARGS_DICT</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">&#39;check-dup-records&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;flag&#39;</span><span class="p">:</span> <span class="s">&#39;-r&#39;</span><span class="p">,</span> <span class="s">&#39;arg&#39;</span><span class="p">:</span> <span class="s">&#39;warn&#39;</span><span class="p">},</span>
    <span class="s">&#39;check-mx&#39;</span><span class="p">:</span>          <span class="p">{</span><span class="s">&#39;flag&#39;</span><span class="p">:</span> <span class="s">&#39;-m&#39;</span><span class="p">,</span> <span class="s">&#39;arg&#39;</span><span class="p">:</span> <span class="s">&#39;warn&#39;</span><span class="p">},</span>
    <span class="s">&#39;check-mx-cname&#39;</span><span class="p">:</span>    <span class="p">{</span><span class="s">&#39;flag&#39;</span><span class="p">:</span> <span class="s">&#39;-M&#39;</span><span class="p">,</span> <span class="s">&#39;arg&#39;</span><span class="p">:</span> <span class="s">&#39;warn&#39;</span><span class="p">},</span>
    <span class="s">&#39;check-srv-cname&#39;</span><span class="p">:</span>   <span class="p">{</span><span class="s">&#39;flag&#39;</span><span class="p">:</span> <span class="s">&#39;-S&#39;</span><span class="p">,</span> <span class="s">&#39;arg&#39;</span><span class="p">:</span> <span class="s">&#39;warn&#39;</span><span class="p">},</span>

    <span class="s">&#39;check-wildcard&#39;</span><span class="p">:</span>    <span class="p">{</span><span class="s">&#39;flag&#39;</span><span class="p">:</span> <span class="s">&#39;-W&#39;</span><span class="p">,</span> <span class="s">&#39;arg&#39;</span><span class="p">:</span> <span class="s">&#39;warn&#39;</span><span class="p">},</span>
    <span class="s">&#39;check-names&#39;</span><span class="p">:</span>       <span class="p">{</span><span class="s">&#39;flag&#39;</span><span class="p">:</span> <span class="s">&#39;-k&#39;</span><span class="p">,</span> <span class="s">&#39;arg&#39;</span><span class="p">:</span> <span class="s">&#39;fail&#39;</span><span class="p">},</span>
    <span class="s">&#39;check-integrity&#39;</span><span class="p">:</span>   <span class="p">{</span><span class="s">&#39;flag&#39;</span><span class="p">:</span> <span class="s">&#39;-i&#39;</span><span class="p">,</span> <span class="s">&#39;arg&#39;</span><span class="p">:</span> <span class="s">&#39;full&#39;</span><span class="p">}</span>
  <span class="p">}</span>

  <span class="c">#Option clauses that we&#39;re interested in, and their default BIND value.</span>
  <span class="n">BIND_OPTION_CLAUSES</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">&#39;check-dup-records&#39;</span><span class="p">:</span> <span class="s">&#39;warn&#39;</span><span class="p">,</span>
    <span class="s">&#39;check-mx&#39;</span><span class="p">:</span>          <span class="s">&#39;warn&#39;</span><span class="p">,</span>
    <span class="s">&#39;check-mx-cname&#39;</span><span class="p">:</span>    <span class="s">&#39;warn&#39;</span><span class="p">,</span>
    <span class="s">&#39;check-srv-cname&#39;</span><span class="p">:</span>   <span class="s">&#39;warn&#39;</span><span class="p">,</span>
    <span class="s">&#39;check-wildcard&#39;</span><span class="p">:</span>    <span class="s">&#39;yes&#39;</span><span class="p">,</span>
    <span class="s">&#39;check-names&#39;</span><span class="p">:</span>       <span class="s">&#39;warn&#39;</span><span class="p">,</span>
    <span class="s">&#39;check-integrity&#39;</span><span class="p">:</span>   <span class="s">&#39;yes&#39;</span><span class="p">,</span>
    <span class="s">&#39;check-siblings&#39;</span><span class="p">:</span>    <span class="s">&#39;yes&#39;</span>
  <span class="p">}</span>

  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">config_file</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">config_file</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="o">.</span><span class="n">SafeConfigParser</span><span class="p">()</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">config_file</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">config_file</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">config_file</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
        <span class="s">&#39;exporter&#39;</span><span class="p">,</span> <span class="s">&#39;root_config_dir&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">config_file</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
        <span class="s">&#39;exporter&#39;</span><span class="p">,</span> <span class="s">&#39;backup_dir&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">max_threads</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">config_file</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
        <span class="s">&#39;exporter&#39;</span><span class="p">,</span> <span class="s">&#39;max_threads&#39;</span><span class="p">))</span>
  
<div class="viewcode-block" id="ConfigLib.UnTarDnsTree"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.UnTarDnsTree">[docs]</a>  <span class="k">def</span> <span class="nf">UnTarDnsTree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">audit_log_id</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Uncompresses the compressed Dns Tree to the </span>
<span class="sd">    root configuration directory.</span>

<span class="sd">    Inputs:</span>
<span class="sd">      audit_log_id: id of the audit log of a compressed dns tree</span>

<span class="sd">    Outputs:</span>
<span class="sd">      audit_log_id: int - the same as the input, unless the input is None.</span>
<span class="sd">      In which case, the most recent audit log will be returned.</span>

<span class="sd">    Raises:</span>
<span class="sd">      ExporterFileError Compressed files will not be extracted to /root/config.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">audit_log_id</span> <span class="ow">is</span> <span class="bp">None</span> <span class="p">):</span>
      <span class="n">audit_log_id</span><span class="p">,</span> <span class="n">filename</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">FindNewestDnsTreeFilename</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="n">filename</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">FindDnsTreeFilename</span><span class="p">(</span><span class="n">audit_log_id</span><span class="p">)</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">filename</span> <span class="ow">is</span> <span class="bp">None</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ExporterNoFileError</span><span class="p">(</span><span class="s">&#39;Could not find a DNS tree for audit </span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> 
                              <span class="n">audit_log_id</span><span class="p">)</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span> <span class="p">):</span>
      <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
      <span class="n">tar_file</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">,</span> <span class="n">filename</span><span class="p">),</span> <span class="s">&#39;r:bz2&#39;</span><span class="p">)</span>
      <span class="n">tar_file</span><span class="o">.</span><span class="n">extractall</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">TarError</span><span class="p">:</span>
      <span class="k">raise</span> <span class="n">ExporterFileError</span><span class="p">(</span><span class="s">&#39;Could not extract the DNS tree </span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s"> to </span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> <span class="p">(</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">))</span>
    <span class="n">tar_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">audit_log_id</span>
</div>
<div class="viewcode-block" id="ConfigLib.TarDnsTree"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.TarDnsTree">[docs]</a>  <span class="k">def</span> <span class="nf">TarDnsTree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">audit_log_id</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Compresses the uncompressed Roster Tree in the root configuration</span>
<span class="sd">        directory to the compressed Roster Tree in the Bind Directory.</span>

<span class="sd">    Inputs:</span>
<span class="sd">      audit_log_id: id of the audit log to a backup tree</span>

<span class="sd">    Raises:</span>
<span class="sd">      ExporterAuditIdError No audit log id supplied.</span>
<span class="sd">      ExporterFileError Can not list files in /dir</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">audit_log_id</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">):</span>
      <span class="k">raise</span> <span class="n">ExporterAuditIdError</span><span class="p">(</span><span class="s">&#39;No audit log id supplied.&#39;</span><span class="p">)</span>
    <span class="n">filename</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">FindDnsTreeFilename</span><span class="p">(</span><span class="n">audit_log_id</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)</span>

    <span class="k">try</span><span class="p">:</span>
      <span class="n">dns_server_files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
      <span class="k">raise</span> <span class="n">ExporterListFileError</span><span class="p">(</span><span class="s">&#39;Can not list files in </span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> 
                                  <span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span>
    <span class="n">temp_tar_name</span> <span class="o">=</span> <span class="s">&#39;tmp_dns_tree.tar.bz2&#39;</span>
    <span class="n">tar_file</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">temp_tar_name</span><span class="p">),</span> 
                            <span class="s">&#39;w:bz2&#39;</span><span class="p">)</span>

    <span class="k">try</span><span class="p">:</span>
      <span class="c"># Files in /root_config_dir</span>
      <span class="c">#   All directories</span>
      <span class="k">for</span> <span class="n">server_dir</span> <span class="ow">in</span> <span class="n">dns_server_files</span><span class="p">:</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">server_dir</span><span class="p">))</span> <span class="p">):</span>
          <span class="c">#Check next directory level for files, and named</span>
          <span class="k">try</span><span class="p">:</span>
            <span class="n">server_files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> 
                                                 <span class="n">server_dir</span><span class="p">))</span>
          <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ExporterListFileError</span><span class="p">(</span><span class="s">&#39;Can not list files in </span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> 
                                    <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">server_dir</span><span class="p">))</span>
          <span class="c"># Files in /root_config_dir/server</span>
          <span class="c">#   One directory (named)</span>
          <span class="c">#   A few files (named_not_compiled.conf, named_compiled.conf, </span>
          <span class="c">#     server.info)</span>
          <span class="k">for</span> <span class="n">server_file</span> <span class="ow">in</span> <span class="n">server_files</span><span class="p">:</span>
            <span class="k">if</span><span class="p">(</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">server_dir</span><span class="p">,</span> 
                                            <span class="n">server_file</span><span class="p">))</span> <span class="p">):</span>
              <span class="k">try</span><span class="p">:</span>
                <span class="n">named_files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> 
                    <span class="n">server_dir</span><span class="p">,</span> <span class="n">server_file</span><span class="p">))</span>
              <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">ExporterListFileError</span><span class="p">(</span><span class="s">&#39;Can not list files in </span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">server_dir</span><span class="p">,</span> <span class="n">server_file</span><span class="p">))</span>
              <span class="c"># Files in /root_config_dir/server/named</span>
              <span class="c">#   All directories</span>
              <span class="k">for</span> <span class="n">view</span> <span class="ow">in</span> <span class="n">named_files</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">__AddToTarFile__</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">server_dir</span><span class="p">,</span> <span class="n">server_file</span><span class="p">,</span>
                    <span class="n">view</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">tar_file</span><span class="p">)</span>
                <span class="k">if</span><span class="p">(</span> <span class="n">view</span> <span class="o">==</span> <span class="s">&#39;named.ca&#39;</span> <span class="p">):</span>
                  <span class="k">continue</span>
                <span class="k">try</span><span class="p">:</span>
                  <span class="n">view_files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span>
                      <span class="n">server_dir</span><span class="p">,</span> <span class="n">server_file</span><span class="p">,</span> <span class="n">view</span><span class="p">))</span>
                <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
                  <span class="k">raise</span> <span class="n">ExporterListFileError</span><span class="p">(</span><span class="s">&#39;Can not list files in &#39;</span>
                      <span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">server_dir</span><span class="p">,</span>
                                        <span class="n">server_file</span><span class="p">,</span> <span class="n">view</span><span class="p">))</span>
                      
                <span class="c"># Files in /root_config_dir/server/named/view</span>
                <span class="c">#   All files</span>
                <span class="k">for</span> <span class="n">zone</span> <span class="ow">in</span> <span class="n">view_files</span><span class="p">:</span>
                  <span class="bp">self</span><span class="o">.</span><span class="n">__AddToTarFile__</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">server_dir</span><span class="p">,</span>
                      <span class="n">server_file</span><span class="p">,</span> <span class="n">view</span><span class="p">,</span> <span class="n">zone</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">tar_file</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">__AddToTarFile__</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">server_dir</span><span class="p">,</span> <span class="n">server_file</span><span class="p">),</span>
                  <span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">tar_file</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">ExporterFileError</span><span class="p">:</span>
      <span class="c"># Removes the temporary tarfile that was created.</span>
      <span class="n">tar_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
      <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">temp_tar_name</span><span class="p">))</span>
      <span class="k">raise</span>
    <span class="n">tar_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="c"># Temporarily moves the original audit file (if htere was one)</span>
    <span class="c">#   in case something happens in transfering the new DNS tree.</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">filename</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="p">):</span>
      <span class="n">shutil</span><span class="o">.</span><span class="n">move</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">,</span> <span class="n">filename</span><span class="p">),</span>
                  <span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.tmp&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">,</span> <span class="n">filename</span><span class="p">))</span>
    <span class="n">date</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span>
    <span class="n">tar_file_name</span> <span class="o">=</span> <span class="s">&#39;dns_tree_</span><span class="si">%s</span><span class="s">-</span><span class="si">%s</span><span class="s">.tar.bz2&#39;</span> <span class="o">%</span> <span class="p">(</span>
        <span class="n">date</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%d</span><span class="s">_%m_%yT%H_%M&#39;</span><span class="p">),</span> <span class="n">audit_log_id</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
      <span class="n">shutil</span><span class="o">.</span><span class="n">move</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">temp_tar_name</span><span class="p">),</span>
                  <span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">,</span> <span class="n">tar_file_name</span><span class="p">))</span>
    <span class="k">except</span> <span class="n">shutil</span><span class="o">.</span><span class="n">Error</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
      <span class="k">if</span><span class="p">(</span> <span class="n">filename</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="p">):</span>
        <span class="n">shutil</span><span class="o">.</span><span class="n">move</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.tmp&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">,</span> <span class="n">filename</span><span class="p">),</span>
                    <span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">,</span> <span class="n">filename</span><span class="p">))</span>
      <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">temp_tar_name</span><span class="p">))</span>
      <span class="k">raise</span> <span class="n">ExporterFileError</span><span class="p">(</span><span class="s">&#39;Unable to move the new DNS tree tar file to &#39;</span>
                              <span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">,</span> <span class="n">tar_file_name</span><span class="p">))</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">filename</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="p">):</span>
      <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.tmp&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">,</span> <span class="n">filename</span><span class="p">))</span>
    <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span>
</div>
  <span class="k">def</span> <span class="nf">__AddToTarFile__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">base_directory</span><span class="p">,</span> <span class="n">tar_file</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Adds file object to tarfile object</span>

<span class="sd">    Inputs:</span>
<span class="sd">      tarfile: tarfile object</span>
<span class="sd">      file_name: path to file object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">tar_file</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">base_directory</span><span class="p">,</span> <span class="n">filename</span><span class="p">),</span> <span class="n">arcname</span><span class="o">=</span><span class="n">filename</span><span class="p">)</span>

<div class="viewcode-block" id="ConfigLib.FindAllDnsServers"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.FindAllDnsServers">[docs]</a>  <span class="k">def</span> <span class="nf">FindAllDnsServers</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Finds and returns the names of all the DNS servers that we are exporting.</span>

<span class="sd">    This must be run after the bind trees are untarred to the </span>
<span class="sd">    root_config_dir.&quot;&quot;&quot;</span>
    <span class="n">server_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">try</span><span class="p">:</span>
      <span class="n">server_list</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
      <span class="k">raise</span> <span class="n">ExporterNoFileError</span><span class="p">(</span><span class="s">&#39;DNS tree does not exist or has not been &#39;</span>
                                <span class="s">&#39;exported yet.&#39;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">server</span> <span class="ow">in</span> <span class="n">server_list</span><span class="p">:</span>
      <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">server</span><span class="p">))</span> <span class="p">):</span>
        <span class="k">raise</span> <span class="n">ExporterFileError</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> is not a server; &#39;</span>
                                <span class="s">&#39;invalid DNS tree format.&#39;</span> <span class="o">%</span> <span class="n">server</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">server_list</span>
</div>
<div class="viewcode-block" id="ConfigLib.FindDnsTreeFilename"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.FindDnsTreeFilename">[docs]</a>  <span class="k">def</span> <span class="nf">FindDnsTreeFilename</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">audit_log_id</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Finds the filename of the Dns Tree from the audit log id given.</span>

<span class="sd">    Inputs:</span>
<span class="sd">      audit_log_id: id of the requested audit log.  </span>

<span class="sd">    Raises:</span>
<span class="sd">      ExporterAuditIdError: No audit log id supplied.</span>
<span class="sd">      ExporterFileError: Can not list files in /dir.</span>
<span class="sd">      ExporterFileError: DNS Tree file /dir/filename is not named correctly.</span>

<span class="sd">    Outputs:</span>
<span class="sd">      string: filename of DNS Tree file</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">audit_log_id</span> <span class="ow">is</span> <span class="bp">None</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ExporterAuditIdError</span><span class="p">(</span><span class="s">&#39;No audit log id supplied.&#39;</span><span class="p">)</span>
    <span class="n">audit_log_id</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">audit_log_id</span>
    <span class="n">file_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">try</span><span class="p">:</span>
      <span class="n">file_list</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
      <span class="k">raise</span> <span class="n">ExporterListFileError</span><span class="p">(</span><span class="s">&#39;Can not list files in </span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">)</span>
    <span class="n">dns_tree_filename</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">for</span> <span class="n">file_name</span> <span class="ow">in</span> <span class="n">file_list</span><span class="p">:</span>
      <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">file_name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;dns_tree&#39;</span><span class="p">)</span> <span class="p">):</span>
        <span class="k">continue</span>
      <span class="k">try</span><span class="p">:</span>
        <span class="n">file_audit_id</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">file_name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;-&#39;</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
      <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ExporterFileNameError</span><span class="p">(</span><span class="s">&#39;DNS Tree file </span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s"> is not named &#39;</span>
            <span class="s">&#39;correctly.&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">,</span> <span class="n">file_name</span><span class="p">))</span>
      <span class="k">if</span><span class="p">(</span> <span class="n">audit_log_id</span><span class="o">==</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">file_audit_id</span> <span class="p">):</span>
        <span class="n">dns_tree_filename</span> <span class="o">=</span> <span class="n">file_name</span>
        <span class="n">newest</span> <span class="o">=</span> <span class="n">file_audit_id</span>
        <span class="k">break</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="n">dns_tree_filename</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">return</span> <span class="n">dns_tree_filename</span>
</div>
<div class="viewcode-block" id="ConfigLib.FindNewestDnsTreeFilename"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.FindNewestDnsTreeFilename">[docs]</a>  <span class="k">def</span> <span class="nf">FindNewestDnsTreeFilename</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Finds the filename of the newest Dns Tree file in the backup directory.</span>

<span class="sd">    Raises:</span>
<span class="sd">      ExporterListFileError: Can not list files in /dir.</span>
<span class="sd">      ExporterNoFileError: Backup directory /dir does not contain any files</span>
<span class="sd">                           or directories.</span>
<span class="sd">      ExporterFileNameError: DNS Tree file /dir/filename is not named correctly.</span>
<span class="sd">      ExporterNoFileError: Could not find a DNS Tree file in /dir.</span>

<span class="sd">    Outputs:</span>
<span class="sd">      tuple: id of audit log, name of Dns Tree file for audit log id</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">file_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">try</span><span class="p">:</span>
      <span class="n">file_list</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
      <span class="k">raise</span> <span class="n">ExporterListFileError</span><span class="p">(</span><span class="s">&#39;Can not list files in </span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">)</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">file_list</span> <span class="o">==</span> <span class="p">[]</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ExporterNoFileError</span><span class="p">(</span><span class="s">&#39;Backup directory </span><span class="si">%s</span><span class="s"> does not contain any files&#39;</span>
                                <span class="s">&#39; or directories.&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">)</span>
    <span class="n">newest</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="n">newest_file_name</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">for</span> <span class="n">file_name</span> <span class="ow">in</span> <span class="n">file_list</span><span class="p">:</span>
      <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">file_name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;dns_tree&#39;</span><span class="p">)</span> <span class="p">):</span>
        <span class="k">continue</span>
      <span class="k">try</span><span class="p">:</span>
        <span class="nb">id</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">file_name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;-&#39;</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
      <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ExporterFileNameError</span><span class="p">(</span><span class="s">&#39;DNS Tree file </span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s"> is not named &#39;</span>
                                    <span class="s">&#39;correctly.&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">,</span> <span class="n">file_name</span><span class="p">))</span>
      <span class="k">if</span><span class="p">(</span> <span class="nb">id</span> <span class="o">&gt;</span> <span class="n">newest</span> <span class="p">):</span>
        <span class="n">newest_file_name</span> <span class="o">=</span> <span class="n">file_name</span>
        <span class="n">newest</span> <span class="o">=</span> <span class="nb">id</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">newest_file_name</span> <span class="ow">is</span> <span class="bp">None</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ExporterNoFileError</span><span class="p">(</span><span class="s">&#39;Could not find a DNS Tree file in </span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span>
                              <span class="bp">self</span><span class="o">.</span><span class="n">backup_dir</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">newest</span><span class="p">,</span> <span class="n">newest_file_name</span>
</div>
<div class="viewcode-block" id="ConfigLib.GetDnsServerInfo"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.GetDnsServerInfo">[docs]</a>  <span class="k">def</span> <span class="nf">GetDnsServerInfo</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Gets the information about a DNS server from the dns_server.info file</span>

<span class="sd">    Inputs:</span>
<span class="sd">      dns_server: name of DNS server</span>

<span class="sd">    Raises:</span>
<span class="sd">      ExporterNoFileError: DNS tree does not exist or has not been </span>
<span class="sd">                           exported yet.</span>
<span class="sd">      ServerCheckError: DNS server server_name does not exist.</span>
<span class="sd">      ServerCheckError: DNS server metadata file does not exist.</span>
<span class="sd">      ServerCheckError: DNS server metadata file incorrectly written.</span>

<span class="sd">    Outputs:</span>
<span class="sd">      dict: {&#39;server_info&#39;: {</span>
<span class="sd">                &#39;server_name&#39;: &#39;university.lcl&#39;,</span>
<span class="sd">                &#39;server_user&#39;: &#39;user&#39;,</span>
<span class="sd">                &#39;bind_dir&#39;: &#39;/etc/bind/&#39;,</span>
<span class="sd">                &#39;test_dir&#39;: &#39;/etc/bind/test/&#39;,</span>
<span class="sd">                &#39;bind_version&#39;: &#39;UNKNOWN&#39;},</span>
<span class="sd">             &#39;tools&#39;: {</span>
<span class="sd">                &#39;tar&#39;: &#39;True&#39;}}</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ExporterNoFileError</span><span class="p">(</span><span class="s">&#39;DNS tree does not exist or has not been &#39;</span>
                                <span class="s">&#39;exported yet.&#39;</span><span class="p">)</span>
    <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">))</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;DNS server </span><span class="si">%s</span><span class="s"> does not exist.&#39;</span> <span class="o">%</span> <span class="n">dns_server</span><span class="p">)</span>
    <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.info&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">,</span>
                                              <span class="n">dns_server</span><span class="p">))</span> <span class="p">):</span>
      <span class="c"># Right now we raise if the server.info file doesn&#39;t exist.</span>
      <span class="c"># Should we just return an empty dictionary {} ?</span>
      <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;DNS server metadata file does not exist.&#39;</span><span class="p">)</span>
    <span class="n">dns_server_info</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="o">.</span><span class="n">SafeConfigParser</span><span class="p">()</span>
    <span class="n">dns_server_info</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.info&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">,</span>
                                            <span class="n">dns_server</span><span class="p">))</span>
    <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">dns_server_info</span><span class="o">.</span><span class="n">has_section</span><span class="p">(</span><span class="s">&#39;server_info&#39;</span><span class="p">)</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;DNS server metadata file incorrectly written.&#39;</span><span class="p">)</span>
    <span class="n">dns_server_options</span> <span class="o">=</span> <span class="n">dns_server_info</span><span class="o">.</span><span class="n">options</span><span class="p">(</span><span class="s">&#39;server_info&#39;</span><span class="p">)</span>
    <span class="k">if</span><span class="p">(</span> <span class="s">&#39;server_name&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">dns_server_options</span> <span class="ow">or</span>
        <span class="n">dns_server_info</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;server_info&#39;</span><span class="p">,</span><span class="s">&#39;server_name&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="n">dns_server</span> <span class="ow">or</span>
        <span class="s">&#39;server_user&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">dns_server_options</span> <span class="ow">or</span>
        <span class="s">&#39;bind_dir&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">dns_server_options</span> <span class="ow">or</span>
        <span class="s">&#39;test_dir&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">dns_server_options</span> <span class="ow">or</span>
        <span class="s">&#39;bind_version&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">dns_server_options</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;DNS server metadata file incorrectly written.&#39;</span><span class="p">)</span>

    <span class="n">server_info_dict</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">section</span> <span class="ow">in</span> <span class="n">dns_server_info</span><span class="o">.</span><span class="n">sections</span><span class="p">():</span>
      <span class="k">if</span><span class="p">(</span> <span class="n">section</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">server_info_dict</span> <span class="p">):</span>
        <span class="n">server_info_dict</span><span class="p">[</span><span class="n">section</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
      <span class="k">for</span> <span class="n">option</span> <span class="ow">in</span> <span class="n">dns_server_info</span><span class="o">.</span><span class="n">options</span><span class="p">(</span><span class="n">section</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">dns_server_info</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">option</span><span class="p">)</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">value</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s">&#39;true&#39;</span> <span class="p">):</span>
          <span class="n">server_info_dict</span><span class="p">[</span><span class="n">section</span><span class="p">][</span><span class="n">option</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="k">elif</span><span class="p">(</span> <span class="n">value</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s">&#39;false&#39;</span> <span class="p">):</span>
          <span class="n">server_info_dict</span><span class="p">[</span><span class="n">section</span><span class="p">][</span><span class="n">option</span><span class="p">]</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="k">else</span><span class="p">:</span>
          <span class="n">server_info_dict</span><span class="p">[</span><span class="n">section</span><span class="p">][</span><span class="n">option</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>

    <span class="k">return</span> <span class="n">server_info_dict</span>
</div>
<div class="viewcode-block" id="ConfigLib.WriteDnsServerInfo"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.WriteDnsServerInfo">[docs]</a>  <span class="k">def</span> <span class="nf">WriteDnsServerInfo</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">server_info_dict</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Writes the information about a DNS server to the dns_server.info file.</span>

<span class="sd">    Inputs:</span>
<span class="sd">      server_info_dict: dict of the DNS server&#39;s information</span>
<span class="sd">                        Example:</span>
<span class="sd">                          {&#39;server_info&#39;: {</span>
<span class="sd">                              &#39;server_name&#39;: &#39;university.lcl&#39;,</span>
<span class="sd">                              &#39;server_user&#39;: &#39;user&#39;,</span>
<span class="sd">                              &#39;bind_dir&#39;: &#39;/etc/bind/&#39;,</span>
<span class="sd">                              &#39;test_dir&#39;: &#39;/etc/bind/test/&#39;,</span>
<span class="sd">                              &#39;bind_version&#39;: &#39;UNKNOWN&#39;},</span>
<span class="sd">                           &#39;tools&#39;: {</span>
<span class="sd">                              &#39;tar&#39;: &#39;True&#39;}}</span>

<span class="sd">    Raises:</span>
<span class="sd">      ExporterNoFileError: DNS tree does not exist or has not been</span>
<span class="sd">                           exported yet.</span>
<span class="sd">      ServerCheckError: DNS server server_name does not exist.</span>
<span class="sd">      ServerCheckError: Invalid DNS server information supplied.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span><span class="p">(</span> <span class="s">&#39;server_info&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">server_info_dict</span> <span class="ow">or</span>
        <span class="s">&#39;server_name&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">server_info_dict</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">]</span> <span class="ow">or</span>
        <span class="s">&#39;server_user&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">server_info_dict</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">]</span> <span class="ow">or</span>
        <span class="s">&#39;bind_dir&#39;</span>    <span class="ow">not</span> <span class="ow">in</span> <span class="n">server_info_dict</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">]</span> <span class="ow">or</span>
        <span class="s">&#39;test_dir&#39;</span>    <span class="ow">not</span> <span class="ow">in</span> <span class="n">server_info_dict</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">]</span> <span class="ow">or</span>
        <span class="s">&#39;bind_version&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">server_info_dict</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">]</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;Invalid DNS server information supplied.&#39;</span><span class="p">)</span>
    <span class="n">dns_server</span> <span class="o">=</span> <span class="n">server_info_dict</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">][</span><span class="s">&#39;server_name&#39;</span><span class="p">]</span>

    <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ExporterNoFileError</span><span class="p">(</span><span class="s">&#39;DNS tree does not exist or has not been &#39;</span>
                                <span class="s">&#39;exported yet.&#39;</span><span class="p">)</span>
    <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">))</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;DNS server </span><span class="si">%s</span><span class="s"> does not exist.&#39;</span> <span class="o">%</span> <span class="n">dns_server</span><span class="p">)</span>

    <span class="n">dns_server_info</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="o">.</span><span class="n">SafeConfigParser</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">section</span> <span class="ow">in</span> <span class="n">server_info_dict</span><span class="p">:</span>
      <span class="n">dns_server_info</span><span class="o">.</span><span class="n">add_section</span><span class="p">(</span><span class="n">section</span><span class="p">)</span>
      <span class="k">for</span> <span class="n">option</span> <span class="ow">in</span> <span class="n">server_info_dict</span><span class="p">[</span><span class="n">section</span><span class="p">]:</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">server_info_dict</span><span class="p">[</span><span class="n">section</span><span class="p">][</span><span class="n">option</span><span class="p">]</span> <span class="o">==</span> <span class="bp">True</span> <span class="p">):</span>
          <span class="n">dns_server_info</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">option</span><span class="p">,</span> <span class="s">&#39;true&#39;</span><span class="p">)</span>
        <span class="k">elif</span><span class="p">(</span> <span class="n">server_info_dict</span><span class="p">[</span><span class="n">section</span><span class="p">][</span><span class="n">option</span><span class="p">]</span> <span class="o">==</span> <span class="bp">False</span> <span class="p">):</span>
          <span class="n">dns_server_info</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">option</span><span class="p">,</span> <span class="s">&#39;false&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
          <span class="n">dns_server_info</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">option</span><span class="p">,</span> 
                              <span class="n">server_info_dict</span><span class="p">[</span><span class="n">section</span><span class="p">][</span><span class="n">option</span><span class="p">])</span>
    <span class="n">dns_server_info_file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.info&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span>
                                                  <span class="n">dns_server</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">),</span> <span class="s">&#39;w&#39;</span><span class="p">)</span>
    <span class="n">dns_server_info</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">dns_server_info_file</span><span class="p">)</span>
    <span class="n">dns_server_info_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
      </div>
<div class="viewcode-block" id="ConfigLib.CheckDnsServer"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.CheckDnsServer">[docs]</a>  <span class="k">def</span> <span class="nf">CheckDnsServer</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">,</span> <span class="n">tools_check_list</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Checks a DNS server for connection, that necessary directories are</span>
<span class="sd">    created, that BIND is installed, and availability of tools.</span>

<span class="sd">    Inputs:</span>
<span class="sd">      dns_server: name of DNS server to check</span>
<span class="sd">      tools_check_list: list of tools to check</span>

<span class="sd">    Raises:</span>
<span class="sd">      ExporterFileError: Can not list files in /dir.</span>
<span class="sd">      ServerCheckError: DNS server 1.2.3.4 does not exist.</span>
<span class="sd">      ServerCheckError: DNS server metadata file incorrectly written or does</span>
<span class="sd">                        not exist.</span>
<span class="sd">      ServerCheckError: Can not connect to 1.2.3.4 via SSH.</span>
<span class="sd">      ServerCheckError: Unable to run &#39;named&#39; on 1.2.3.4.  Is BIND installed</span>
<span class="sd">                        on the remote server?</span>
<span class="sd">      ServerCheckError: The remote test directory /etc/bind/test does not exist</span>
<span class="sd">                        or the user someone does not have permission.</span>
<span class="sd">      ServerCheckError: The remote BIND directory /etc/bind does not exist or</span>
<span class="sd">                        the user someone does not have permission.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">output</span> <span class="ow">in</span> <span class="n">fabric_state</span><span class="o">.</span><span class="n">output</span><span class="p">:</span>
      <span class="n">fabric_state</span><span class="o">.</span><span class="n">output</span><span class="p">[</span><span class="n">output</span><span class="p">]</span> <span class="o">=</span> <span class="bp">False</span>

    <span class="n">dns_server_data</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">try</span><span class="p">:</span>
      <span class="n">dns_servers</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
      <span class="k">raise</span> <span class="n">ExporterListFileError</span><span class="p">(</span><span class="s">&#39;Can not list files in </span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> 
                                  <span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">dns_server</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">dns_servers</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;DNS server </span><span class="si">%s</span><span class="s"> does not exist.&#39;</span> <span class="o">%</span> <span class="n">dns_server</span><span class="p">)</span>
    
    <span class="n">dns_server_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">GetDnsServerInfo</span><span class="p">(</span><span class="n">dns_server</span><span class="p">)</span>
    <span class="n">ssh_host</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">@</span><span class="si">%s</span><span class="s">:22&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">][</span><span class="s">&#39;server_user&#39;</span><span class="p">],</span>
                             <span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">][</span><span class="s">&#39;server_name&#39;</span><span class="p">])</span>
    <span class="k">try</span><span class="p">:</span>
      <span class="n">fabric_api</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">warn_only</span> <span class="o">=</span> <span class="bp">False</span>

      <span class="c"># Testing for server connection through SSH</span>
      <span class="n">fabric_api</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">host_string</span> <span class="o">=</span> <span class="n">ssh_host</span>
      <span class="n">test_return</span> <span class="o">=</span> <span class="n">fabric_api</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s">&#39;echo &quot;Test&quot;&#39;</span><span class="p">)</span>
      <span class="k">if</span><span class="p">(</span> <span class="n">test_return</span> <span class="o">!=</span> <span class="s">&#39;Test&#39;</span> <span class="p">):</span>
        <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;Can not connect to </span><span class="si">%s</span><span class="s"> via SSH.&#39;</span> <span class="o">%</span> <span class="n">dns_server</span><span class="p">)</span>
    
      <span class="n">fabric_api</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">warn_only</span> <span class="o">=</span> <span class="bp">True</span>

      <span class="c"># Testing for BIND installation</span>
      <span class="n">result</span> <span class="o">=</span> <span class="n">fabric_api</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s">&#39;named -v&#39;</span><span class="p">)</span>
      <span class="k">if</span><span class="p">(</span> <span class="s">&#39;command not found&#39;</span> <span class="ow">in</span> <span class="n">result</span> <span class="p">):</span>
        <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;Unable to run </span><span class="se">\&#39;</span><span class="s">named</span><span class="se">\&#39;</span><span class="s"> on </span><span class="si">%s</span><span class="s">.  Is BIND &#39;</span>
                               <span class="s">&#39;installed on the remote server?&#39;</span> <span class="o">%</span> <span class="n">dns_server</span><span class="p">)</span>
      <span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">][</span><span class="s">&#39;bind_version&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s">&#39;BIND &#39;</span><span class="p">)</span>

      <span class="c"># Testing for directories</span>
      <span class="n">result</span> <span class="o">=</span> <span class="n">fabric_api</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s">&#39;ls </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> 
                              <span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">][</span><span class="s">&#39;bind_dir&#39;</span><span class="p">])</span>
      <span class="k">if</span><span class="p">(</span> <span class="s">&#39;No such file or directory&#39;</span> <span class="ow">in</span> <span class="n">result</span> <span class="p">):</span>
        <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;The remote BIND directory </span><span class="si">%s</span><span class="s"> does not exist &#39;</span>
            <span class="s">&#39;or the user </span><span class="si">%s</span><span class="s"> does not have permission.&#39;</span> <span class="o">%</span> 
            <span class="p">(</span><span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">][</span><span class="s">&#39;bind_dir&#39;</span><span class="p">],</span>
             <span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">][</span><span class="s">&#39;server_user&#39;</span><span class="p">]))</span>

      <span class="n">result</span> <span class="o">=</span> <span class="n">fabric_api</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s">&#39;ls </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> 
                              <span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">][</span><span class="s">&#39;test_dir&#39;</span><span class="p">])</span>
      <span class="k">if</span><span class="p">(</span> <span class="s">&#39;No such file or directory&#39;</span> <span class="ow">in</span> <span class="n">result</span> <span class="p">):</span>
        <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;The remote test directory </span><span class="si">%s</span><span class="s"> does not exist &#39;</span>
            <span class="s">&#39;or the user </span><span class="si">%s</span><span class="s"> does not have permission.&#39;</span> <span class="o">%</span> 
            <span class="p">(</span><span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">][</span><span class="s">&#39;test_dir&#39;</span><span class="p">],</span> 
             <span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;server_info&#39;</span><span class="p">][</span><span class="s">&#39;server_user&#39;</span><span class="p">]))</span>
    
      <span class="c"># Testing for tools</span>
      <span class="k">if</span><span class="p">(</span> <span class="s">&#39;tools&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">dns_server_info</span> <span class="p">):</span>
        <span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;tools&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
      <span class="k">for</span> <span class="n">tool</span> <span class="ow">in</span> <span class="n">tools_check_list</span><span class="p">:</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">tool</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">ToolList</span> <span class="p">):</span>
          <span class="k">raise</span> <span class="n">errors</span><span class="o">.</span><span class="n">FunctionError</span><span class="p">(</span><span class="s">&#39;Provided tool </span><span class="si">%s</span><span class="s"> is not in tool list.&#39;</span>
                                     <span class="o">%</span> <span class="n">tool</span><span class="p">)</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">fabric_api</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">tool</span><span class="p">)</span>
        <span class="k">if</span><span class="p">(</span> <span class="s">&#39;command not found&#39;</span> <span class="ow">in</span> <span class="n">result</span> <span class="p">):</span>
          <span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;tools&#39;</span><span class="p">][</span><span class="n">tool</span><span class="p">]</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="k">else</span><span class="p">:</span>
          <span class="n">dns_server_info</span><span class="p">[</span><span class="s">&#39;tools&#39;</span><span class="p">][</span><span class="n">tool</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">WriteDnsServerInfo</span><span class="p">(</span><span class="n">dns_server_info</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">FabricNetworkError</span><span class="p">:</span>
      <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;Could not connect to </span><span class="si">%s</span><span class="s"> via SSH.&#39;</span> <span class="o">%</span> <span class="n">dns_server</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">ConfigParser</span><span class="o">.</span><span class="n">Error</span><span class="p">:</span>
      <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;Can not write to </span><span class="si">%s</span><span class="s"> info file.&#39;</span> <span class="o">%</span> <span class="n">dns_server</span><span class="p">)</span>
    <span class="k">finally</span><span class="p">:</span>
      <span class="n">fabric_network</span><span class="o">.</span><span class="n">disconnect_all</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="ConfigLib.TarDnsBindFiles"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.TarDnsBindFiles">[docs]</a>  <span class="k">def</span> <span class="nf">TarDnsBindFiles</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Tars the BIND tree for a DNS server.</span>
<span class="sd">    </span>
<span class="sd">    Inputs:</span>
<span class="sd">      dns_server: name of dns_server</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">)</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ExporterNoFileError</span><span class="p">(</span><span class="s">&#39;DNS tree does not exist or has not been &#39;</span>
                                <span class="s">&#39;exported yet.&#39;</span><span class="p">)</span>
    <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">))</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;DNS server </span><span class="si">%s</span><span class="s"> does not exist.&#39;</span> <span class="o">%</span> <span class="n">dns_server</span><span class="p">)</span>
    <span class="n">dns_tar_file</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.tar.bz2&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span>
        <span class="n">dns_server</span><span class="p">,</span><span class="n">dns_server</span><span class="p">),</span> <span class="s">&#39;w:bz2&#39;</span><span class="p">)</span>

    <span class="k">try</span><span class="p">:</span>
      <span class="n">server_files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">))</span>
      <span class="k">for</span> <span class="n">server_file</span> <span class="ow">in</span> <span class="n">server_files</span><span class="p">:</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">,</span> 
                                        <span class="n">server_file</span><span class="p">))</span> <span class="p">):</span>
          <span class="k">try</span><span class="p">:</span>
            <span class="n">named_files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span>
                <span class="n">dns_server</span><span class="p">,</span> <span class="n">server_file</span><span class="p">))</span>
          <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ExporterListFileError</span><span class="p">(</span><span class="s">&#39;Can not list files in </span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span>
                <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">,</span> <span class="n">server_file</span><span class="p">))</span>
          <span class="k">for</span> <span class="n">view</span> <span class="ow">in</span> <span class="n">named_files</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__AddToTarFile__</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">server_file</span><span class="p">,</span> <span class="n">view</span><span class="p">),</span>
                <span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">),</span> <span class="n">dns_tar_file</span><span class="p">)</span>
            <span class="k">if</span><span class="p">(</span> <span class="n">view</span> <span class="o">==</span> <span class="s">&#39;named.ca&#39;</span> <span class="p">):</span>
              <span class="k">continue</span>
            <span class="k">try</span><span class="p">:</span>
              <span class="n">view_files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span>
                  <span class="n">dns_server</span><span class="p">,</span> <span class="n">server_file</span><span class="p">,</span> <span class="n">view</span><span class="p">))</span>
            <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
              <span class="k">raise</span> <span class="n">ExporterListFileError</span><span class="p">(</span><span class="s">&#39;Invalid tree format, can not list &#39;</span>
                  <span class="s">&#39;files in </span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">,</span>
                                             <span class="n">server_file</span><span class="p">,</span> <span class="n">view</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">zone</span> <span class="ow">in</span> <span class="n">view_files</span><span class="p">:</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">__AddToTarFile__</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">server_file</span><span class="p">,</span>
                  <span class="n">view</span><span class="p">,</span> <span class="n">zone</span><span class="p">),</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">),</span> 
                  <span class="n">dns_tar_file</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">__AddToTarFile__</span><span class="p">(</span><span class="n">server_file</span><span class="p">,</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span> 
              <span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">),</span> <span class="n">dns_tar_file</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">ExporterFileError</span><span class="p">:</span>
      <span class="n">dns_tar_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
      <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">.tar.bz2&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">,</span> 
                                      <span class="n">dns_server</span><span class="p">))</span>
      <span class="k">raise</span>
    <span class="n">dns_tar_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

  <span class="c"># There are 2 ways to get zones.  By reading the zone files, or by named.conf</span>
  <span class="c">#   This uses the first.  Opens more files, but has less room for mistakes</span>
  <span class="c">#   because there is less parsing that is done.</span></div>
<div class="viewcode-block" id="ConfigLib.GetZoneList"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.GetZoneList">[docs]</a>  <span class="k">def</span> <span class="nf">GetZoneList</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Gets all of the zones from a BIND tree for a DNS server.</span>

<span class="sd">    Inputs:</span>
<span class="sd">      dns_server: name of a DNS server</span>

<span class="sd">    Outputs:</span>
<span class="sd">      dict: {&#39;view1&#39;: {&#39;zone1.lcl&#39;: &#39;zone.db&#39;, &#39;zone2&#39;.lcl: &#39;zone.db&#39;},</span>
<span class="sd">             &#39;view2&#39;: {&#39;zone3.lcl&#39;: &#39;zone.db&#39;, &#39;zone4.lcl&#39;: &#39;zone.db&#39;}}</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
      <span class="n">views</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/named/&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">))</span>
      <span class="n">zone_dict</span> <span class="o">=</span> <span class="p">{}</span>
      <span class="k">for</span> <span class="n">view</span> <span class="ow">in</span> <span class="n">views</span><span class="p">:</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">view</span> <span class="o">==</span> <span class="s">&#39;named.ca&#39;</span> <span class="p">):</span>
          <span class="k">continue</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">view</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">zone_dict</span> <span class="p">):</span>
          <span class="n">zone_dict</span><span class="p">[</span><span class="n">view</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">zone_files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/named/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> 
                                               <span class="n">dns_server</span><span class="p">,</span> <span class="n">view</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">zone_file_name</span> <span class="ow">in</span> <span class="n">zone_files</span><span class="p">:</span>
          <span class="n">zone_file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">/named/</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> 
              <span class="n">dns_server</span><span class="p">,</span> <span class="n">view</span><span class="p">,</span> <span class="n">zone_file_name</span><span class="p">))</span>
          <span class="n">zone_origin</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
          <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">zone_file</span><span class="p">:</span>
            <span class="k">if</span><span class="p">(</span> <span class="s">&#39;ORIGIN&#39;</span> <span class="ow">in</span> <span class="n">line</span> <span class="p">):</span>
              <span class="n">zone_origin</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s">&#39;$ORIGIN &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">&#39;.</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
              <span class="k">break</span>
          <span class="k">else</span><span class="p">:</span>
            <span class="n">zone_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
            <span class="k">raise</span> <span class="n">errors</span><span class="o">.</span><span class="n">FunctionError</span><span class="p">(</span><span class="s">&#39;Invalid zone file format for </span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> 
                                       <span class="n">zone_file_name</span><span class="p">)</span>
          <span class="n">zone_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
          <span class="n">zone_dict</span><span class="p">[</span><span class="n">view</span><span class="p">][</span><span class="n">zone_origin</span><span class="p">]</span> <span class="o">=</span> <span class="n">zone_file_name</span>
    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
      <span class="k">raise</span> <span class="n">ServerCheckError</span><span class="p">(</span><span class="s">&#39;Can not list files in </span><span class="si">%s</span><span class="s">.&#39;</span> <span class="o">%</span> <span class="n">err</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">zone_dict</span>
</div>
<div class="viewcode-block" id="ConfigLib.MergeOptionsDicts"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.MergeOptionsDicts">[docs]</a>  <span class="k">def</span> <span class="nf">MergeOptionsDicts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">global_options_dict</span><span class="p">,</span> <span class="n">view_dict</span><span class="p">,</span> <span class="n">zone_dict</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Merges the options of 3 dictionaries, according to precendence.</span>
<span class="sd">    zone_dict is the most importantce, while global_options_dict has the &quot;least&quot;</span>
<span class="sd">    importantance.</span>
<span class="sd">    </span>
<span class="sd">      Inputs:</span>
<span class="sd">        global_options_dict: dict of the options defined globally in named.conf</span>
<span class="sd">        view_dict: dict of the options defined in a single view of named.conf</span>
<span class="sd">        zone_dict: dict of the options defined in a single zone of named.conf</span>

<span class="sd">      Outputs:</span>
<span class="sd">        single_options_dict: dict of the merged options</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">single_options_dict</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="c">#Go in order of least important to most important</span>
    <span class="k">for</span> <span class="n">options_dict</span> <span class="ow">in</span> <span class="p">[</span><span class="n">global_options_dict</span><span class="p">,</span> <span class="n">view_dict</span><span class="p">,</span> <span class="n">zone_dict</span><span class="p">]:</span>
      <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">options_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">BIND_OPTION_CLAUSES</span> <span class="p">):</span>
          <span class="n">single_options_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">options_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>

    <span class="c">#If we didn&#39;t fill in any of the options, we want to fill in their defaults</span>
    <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">BIND_OPTION_CLAUSES</span><span class="p">:</span>
      <span class="k">if</span><span class="p">(</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">single_options_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span> <span class="p">):</span>
        <span class="n">single_options_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">BIND_OPTION_CLAUSES</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>

    <span class="k">return</span> <span class="n">single_options_dict</span>
</div>
<div class="viewcode-block" id="ConfigLib.GenerateAdditionalNamedCheckzoneArgs"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.GenerateAdditionalNamedCheckzoneArgs">[docs]</a>  <span class="k">def</span> <span class="nf">GenerateAdditionalNamedCheckzoneArgs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bind_options_dict</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Generates additional named-checkzone command line flags and arguments based</span>
<span class="sd">    on the BIND options supplied in bind_options_dict.</span>
<span class="sd">    </span>
<span class="sd">    Inputs:</span>
<span class="sd">      bind_options_dict: dict of BIND options parsed from named.conf</span>
<span class="sd">      </span>
<span class="sd">    Outputs:</span>
<span class="sd">      args: list of flags and their args.</span>
<span class="sd">    &quot;&quot;&quot;</span>
     
    <span class="n">args</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">check_siblings</span> <span class="o">=</span> <span class="bp">False</span>

    <span class="k">for</span> <span class="n">option_key</span><span class="p">,</span> <span class="n">option_value</span> <span class="ow">in</span> <span class="n">bind_options_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
      <span class="k">if</span><span class="p">(</span> <span class="n">option_key</span> <span class="o">!=</span> <span class="s">&#39;check-siblings&#39;</span> <span class="p">):</span>
        <span class="n">checkzone_arg</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">option_value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">COMMANDS_CONVERT_DICT</span><span class="p">[</span><span class="n">option_key</span><span class="p">]</span> <span class="p">):</span>
          <span class="n">checkzone_arg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">COMMANDS_CONVERT_DICT</span><span class="p">[</span><span class="n">option_key</span><span class="p">][</span><span class="n">option_value</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
          <span class="c">#These checks are for global and view options</span>
          <span class="c">#In some clauses, fail, warn, and ignore are not enough.</span>
          <span class="c">#master/slave/response warn/fail/ignore is the correct syntax.</span>
          <span class="k">if</span><span class="p">(</span> <span class="s">&#39;fail&#39;</span> <span class="ow">in</span> <span class="n">option_value</span> <span class="p">):</span>
            <span class="n">checkzone_arg</span> <span class="o">=</span> <span class="s">&#39;fail&#39;</span>
          <span class="k">elif</span><span class="p">(</span> <span class="s">&#39;warn&#39;</span> <span class="ow">in</span> <span class="n">option_value</span> <span class="p">):</span>
            <span class="n">checkzone_arg</span> <span class="o">=</span> <span class="s">&#39;warn&#39;</span>
          <span class="k">elif</span><span class="p">(</span> <span class="s">&#39;ignore&#39;</span> <span class="ow">in</span> <span class="n">option_value</span> <span class="p">):</span>
            <span class="n">checkzone_arg</span> <span class="o">=</span> <span class="s">&#39;ignore&#39;</span>
          <span class="k">else</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&#39;ERROR: Unknown option &quot;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">option_key</span><span class="p">,</span> <span class="n">option_value</span><span class="p">)</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">CHECKZONE_ARGS_DICT</span><span class="p">[</span><span class="n">option_key</span><span class="p">][</span><span class="s">&#39;arg&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">checkzone_arg</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">option_value</span> <span class="o">==</span> <span class="s">&#39;yes&#39;</span> <span class="p">):</span>
          <span class="n">check_siblings</span> <span class="o">=</span> <span class="bp">True</span>

    <span class="c">#if we want to check siblings, and if we are checking integrity,</span>
    <span class="c">#append &#39;-sibling&#39; to the -i arg of the CHECKZONE_ARGS_DICT</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">check_siblings</span> <span class="ow">and</span> 
        <span class="bp">self</span><span class="o">.</span><span class="n">CHECKZONE_ARGS_DICT</span><span class="p">[</span><span class="s">&#39;check-integrity&#39;</span><span class="p">][</span><span class="s">&#39;arg&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="s">&#39;none&#39;</span> <span class="p">):</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">CHECKZONE_ARGS_DICT</span><span class="p">[</span><span class="s">&#39;check-integrity&#39;</span><span class="p">][</span><span class="s">&#39;arg&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">-sibling&#39;</span> <span class="o">%</span> <span class="p">(</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">CHECKZONE_ARGS_DICT</span><span class="p">[</span><span class="s">&#39;check-integrity&#39;</span><span class="p">][</span><span class="s">&#39;arg&#39;</span><span class="p">])</span>

    <span class="c">#Transferring everything from CHECKZONE_ARGS_DICT to args list</span>
    <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">CHECKZONE_ARGS_DICT</span><span class="p">:</span>
      <span class="n">flag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">CHECKZONE_ARGS_DICT</span><span class="p">[</span><span class="n">key</span><span class="p">][</span><span class="s">&#39;flag&#39;</span><span class="p">]</span>
      <span class="n">arg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">CHECKZONE_ARGS_DICT</span><span class="p">[</span><span class="n">key</span><span class="p">][</span><span class="s">&#39;arg&#39;</span><span class="p">]</span>

      <span class="n">args</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">flag</span><span class="p">,</span> <span class="n">arg</span><span class="p">])</span>

    <span class="k">return</span> <span class="n">args</span>
</div>
<div class="viewcode-block" id="ConfigLib.GetNamedZoneToolArgs"><a class="viewcode-back" href="../../roster_config_manager.html#roster_config_manager.config_lib.ConfigLib.GetNamedZoneToolArgs">[docs]</a>  <span class="k">def</span> <span class="nf">GetNamedZoneToolArgs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">,</span> <span class="n">view</span><span class="p">,</span> <span class="n">zone_file_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Generates the arg flags for named-checkzone and named-compilezone</span>
<span class="sd">    for a specific zone-view-server combination.</span>
<span class="sd">     </span>
<span class="sd">     Inputs:</span>
<span class="sd">       dns_server: name of a DNS server</span>
<span class="sd">       view: name of a view</span>
<span class="sd">       zone_file_name: the file name of a zone</span>
<span class="sd">     </span>
<span class="sd">     Outputs:</span>
<span class="sd">       string: string of the command flags and their values for named-checkzone</span>
<span class="sd">               and named-compilezone.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">zone_name</span> <span class="o">=</span> <span class="n">zone_file_name</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">&#39;.db&#39;</span><span class="p">)</span>
    <span class="n">server_directory</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root_config_dir</span><span class="p">,</span> <span class="n">dns_server</span><span class="p">)</span>
    <span class="n">named_file_name</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">/named.conf.a&#39;</span> <span class="o">%</span> <span class="n">server_directory</span>
    <span class="k">try</span><span class="p">:</span>
      <span class="n">named_file_handle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">named_file_name</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">)</span>
      <span class="n">named_file_string</span> <span class="o">=</span> <span class="n">named_file_handle</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
    <span class="k">finally</span><span class="p">:</span>
      <span class="n">named_file_handle</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="n">named_file_dict</span> <span class="o">=</span> <span class="n">iscpy</span><span class="o">.</span><span class="n">ParseISCString</span><span class="p">(</span><span class="n">named_file_string</span><span class="p">)</span>
    <span class="n">global_options_dict</span> <span class="o">=</span> <span class="n">named_file_dict</span><span class="p">[</span><span class="s">&#39;options&#39;</span><span class="p">]</span>

    <span class="k">if</span><span class="p">(</span> <span class="s">&#39;view &quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="n">view</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">named_file_dict</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">ConfigManagerError</span><span class="p">(</span><span class="s">&#39;Could not find view </span><span class="si">%s</span><span class="s"> in named.conf&#39;</span> <span class="o">%</span> <span class="n">view</span><span class="p">)</span>
    <span class="n">view_dict</span> <span class="o">=</span> <span class="n">named_file_dict</span><span class="p">[</span><span class="s">&#39;view &quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="n">view</span><span class="p">]</span>
    
    <span class="n">zone_dict</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">for</span> <span class="n">zone</span> <span class="ow">in</span> <span class="n">view_dict</span><span class="p">:</span>
      <span class="c">#Making sure we&#39;re checking a dictionary</span>
      <span class="k">if</span><span class="p">(</span> <span class="nb">type</span><span class="p">(</span><span class="n">view_dict</span><span class="p">[</span><span class="n">zone</span><span class="p">])</span> <span class="o">==</span> <span class="nb">type</span><span class="p">({})</span> <span class="p">):</span>
        <span class="k">if</span><span class="p">(</span> <span class="s">&#39;file&#39;</span> <span class="ow">in</span> <span class="n">view_dict</span><span class="p">[</span><span class="n">zone</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span> <span class="p">):</span>
          <span class="k">if</span><span class="p">(</span> <span class="n">view_dict</span><span class="p">[</span><span class="n">zone</span><span class="p">][</span><span class="s">&#39;file&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s">&#39;&quot;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">zone_file_name</span><span class="p">)</span> <span class="p">):</span>
            <span class="n">zone_dict</span> <span class="o">=</span> <span class="n">view_dict</span><span class="p">[</span><span class="n">zone</span><span class="p">]</span>
            <span class="k">break</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="k">raise</span> <span class="n">ConfigManagerError</span><span class="p">(</span><span class="s">&#39;Could not find zone </span><span class="si">%s</span><span class="s"> in view </span><span class="si">%s</span><span class="s"> within named.conf&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">zone_name</span><span class="p">,</span> <span class="n">view</span><span class="p">))</span>

    <span class="n">options_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">MergeOptionsDicts</span><span class="p">(</span>
        <span class="n">global_options_dict</span><span class="p">,</span> <span class="n">view_dict</span><span class="p">,</span> <span class="n">zone_dict</span><span class="p">)</span>
    <span class="n">additional_args</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">GenerateAdditionalNamedCheckzoneArgs</span><span class="p">(</span><span class="n">options_dict</span><span class="p">)</span>

    <span class="k">return</span> <span class="s">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">additional_args</span><span class="p">)</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">Roster Config Manager v0.18 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2013, Purdue University.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
    </div>
  </body>
</html>